package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.CommentSign;
import cn.wolfcode.trip.base.domain.Information;
import cn.wolfcode.trip.base.domain.TravelSign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelSignMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IInformationService;
import cn.wolfcode.trip.base.service.ITravelSignService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelSignServiceImpl implements ITravelSignService {
    @Autowired
    private TravelSignMapper travelSignMapper;

    @Autowired
    private IInformationService informationService;


    @Override
    public TravelSign get(Long id) {
        return travelSignMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TravelSign> listAll() {
        return travelSignMapper.selectAll();
    }

    @Override
    public void save(TravelSign travelSign) {
        //设置点赞或者收藏的时间
        if (travelSign.getGreatStatus() != null) {
            travelSign.setGreatTime(new Date());
        }
        if (travelSign.getEnshrineStatus() != null) {
            travelSign.setEnshrineTime(new Date());
        }
        travelSignMapper.insert(travelSign);
        saveInfoRela(travelSign);

    }

    private void saveInfoRela(TravelSign travelSign) {
        User currentUser = UserContext.getCurrentUser();
        Information information = new Information();
        User author = travelSign.getAuthor();
        if (travelSign.getType() == 0 && travelSign.getGreatStatus()) {
            information.setUser(currentUser);
            information.setType(Information.USER_MESSAGE_LIKE);
            information.setTitle("游记");
            information.setContent(currentUser.getNickName() + "给您的游记点赞了");
            informationService.saveUserInformation(information, author);
        }
        if (travelSign.getType() == 1 && travelSign.getEnshrineStatus()) {
            information.setUser(currentUser);
            information.setType(Information.USER_MESSAGE_COLLECTION);
            information.setTitle("游记");
            information.setContent(currentUser.getNickName() + "收藏了您的评论");
            informationService.saveUserInformation(information, author);
        }

    }

    @Override
    public void update(TravelSign travelSign) {
        //设置点赞或者收藏的时间
        if (travelSign.getGreatStatus() != null) {
            travelSign.setGreatTime(new Date());
        }
        if (travelSign.getEnshrineStatus() != null) {
            travelSign.setEnshrineTime(new Date());
        }
        travelSignMapper.updateByPrimaryKey(travelSign);

    }


    @Override
    public PageInfo queryForList(QueryObject qo) {
        return null;
    }

    @Override
    public void updateState(TravelSign travelSign) {

    }


    /**
     * 根据大攻略id查询当前用户的在该攻略下的标记状态
     *
     * @param travelId
     * @return
     */
    @Override
    public TravelSign selectByTravelId(Long travelId, Long userId) {

        return travelSignMapper.selectByTravelId(travelId, userId);
    }

    @Override
    public int queryGreatStatusCountByTravelId(Long travelId) {

        return travelSignMapper.queryGreatStatusCountByTravelId(travelId);
    }

    @Override
    public int queryEnshrineStatusCountByTravelId(Long travelId) {
        return travelSignMapper.queryEnshrineStatusCountByTravelId(travelId);
    }


}
